package com.kkwrite.regimen.provider.controller.message;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.provider.service.message.ShopMessageService;
import com.kkwrite.regimen.provider.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_FALSE;
import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_TRUE;

/**
 * Created by Ron on 2018/8/26 12:14.
 *
 * @author Ron
 */
@Api(value = "店铺消息模块")
@RestController
@RequestMapping(value = "/shopmessage")
public class ShopMessageController {

    @Resource(name = "tokenUtil")
    private TokenUtil tokenUtil;
    @Resource(name = "shopMessageService")
    private ShopMessageService service;

    @GetMapping(value = "/querypageable")
    @ApiOperation(value = "分页查询店铺消息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条目数，大于等于1", required = true, paramType = "query", dataType = "int")
    })
    public OutDTO findPageable(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestHeader @ApiIgnore String token) {
        String shopIdString = (String) tokenUtil.getValue(token, TokenUtil.TokenKeyEnum.SHOP_ID);
        if (shopIdString == null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "002", "token invalid");
        }
        Integer shopId = Integer.valueOf(shopIdString);
        if (page == null || page < 0 || pageSize == null || pageSize < 1) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        try {
            return OutDTO.of(BOOLEAN_STRING_TRUE, "OK", "OK", service.queryPageable(shopId, page, pageSize));
        } catch (Exception e) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "服务异常！异常信息：" + e.getMessage());
        }
    }

}
